package com.xpmusic.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.xpmusic.dao.impl.AdminDaoImpl;
import com.xpmusic.dao.impl.ConsumerDaoImpl;
import com.xpmusic.model.AdminInfo;
import com.xpmusic.model.ConsumerInfo;
import com.xpmusic.util.ServletUtil;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        //这里把通过流数据读取字符串数据封装起来，直接调用
        String inputStringInfo = ServletUtil.getInputStringInfo(request);
        System.out.println("json: " + inputStringInfo);

        //将客户端发送的数据解析为json对象
        Gson gson = new Gson();
        JsonObject jsonObject = new JsonParser().parse(inputStringInfo).getAsJsonObject();
        JsonObject result = new JsonObject();
        boolean b = false;
        //根据接收到的type类型，来判断是普通用户还是管理员，1代表管理员，0代表普通用户
        int i = jsonObject.get("type").getAsInt();
        AdminInfo adminInfo = gson.fromJson(inputStringInfo,AdminInfo.class);
        if(i == 1){
            b = new AdminDaoImpl().checkAdminInfo(adminInfo);
        }else if(i == 0){
            //首先验证验证码是否正确
            if(jsonObject.get("code").getAsString().equalsIgnoreCase(request.getSession().getAttribute("code").toString())){
                ConsumerInfo consumerInfo = gson.fromJson(inputStringInfo, ConsumerInfo.class);
                b = new ConsumerDaoImpl().checkConsumerInfo(consumerInfo);
            }else{
                response.setContentType("text/json;charset=utf-8");
                result.addProperty("code","false");
                response.getWriter().write(result.toString());
                return;
            }
        }

        System.out.println("LoginServlet-result:  " + b);
        //根据不同的用户，登陆成功后跳转的页面也不同，普通用户是personalMusic.jsp，管理员用户是musicList.jsp
        if(b){
            response.setContentType("text/json;charset=utf-8");
            result.addProperty("result","true");
            if(i == 1){
                result.addProperty("url","musicList.jsp");
                request.getSession().setAttribute("admin",adminInfo);
            }
            if(i == 0){
                request.getSession().setAttribute("loginName",jsonObject.get("loginName").getAsString());
                result.addProperty("url","personalMusic.jsp");
            }
            response.getWriter().write(result.toString());
        }else {
            response.setContentType("text/json;charset=utf-8");
            result.addProperty("result","false");
            result.addProperty("url","error.jsp");
            response.getWriter().write(result.toString());
        }
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }
}
